package Demo3;

import java.io.*;
import java.util.Random;

public class CreateFile extends Thread {
    private char name;// 名字
    public CreateFile(char name) {
        this.name = name;
    }
    private static final Object lock = new Object();

    @Override
    public void run() {
        Random random = new Random();
        synchronized (lock) {
            // 创建文件
            File file1 = new File("D:\\text\\" + name);
            file1.mkdir();
            for(int i = 1; i <= 10000;i++) {
                // 创建10000个文件
                String str = "D:\\text\\" + name + "\\" + name + i + ".txt";
                File file2 = new File(str);
                try {
                    file2.createNewFile();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                // 输入字母
                try {
                    FileOutputStream fileOutputStream = new FileOutputStream(str);
                    for(int j = 0; j < 10;j++) {
                        fileOutputStream.write((char)('a' + random.nextInt(26)));
                    }
                    fileOutputStream.close();
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        }
}